package com.lianzhihui.romeex.utils;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Description: 字符串工具
 * Author: RAMON
 * CreateDate: 3/23/21 6:13 PM
 * UpdateUser:
 * UpdateDate:
 * UpdateRemark:
 * Version:
 */
public class StringUtils {

    /**
     * 两种颜色 设置
     *
     * @param textview 设置对象
     * @param str1     文字1
     * @param color1   颜色1
     * @param str2     文字2
     * @param color2   颜色2
     */
    public static void setString(@NonNull TextView textview, @NonNull String str1, @NonNull String color1,
                                 @NonNull String str2, @NonNull String color2) {

        String content = str1 + str2;
        SpannableStringBuilder ssb = new SpannableStringBuilder(content);

        ForegroundColorSpan span1 = new ForegroundColorSpan(Color.parseColor(color1));
        int indexStart1 = 0;
        int indexEnd1 = str1.length() - 1;

        ForegroundColorSpan span2 = new ForegroundColorSpan(Color.parseColor(color2));
        int indexStart2 = indexEnd1 + 1;
        int indexEnd2 = indexStart2 + str2.length();

//        Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到终了下标，包括起始下标
//        Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到终了下标，同时包括起始下标和终了下标
//        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终了下标，但都不包括起始下标和终了下标
//        Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到终了下标，包括终了下标

        ssb.setSpan(span1, indexStart1, indexEnd1, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ssb.setSpan(span2, indexStart2, indexEnd2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        textview.setText(ssb);

    }

    /**
     * 多文字，颜色
     *
     * @param textview
     * @param texts
     * @param colors
     */
    public static void setString(@NonNull TextView textview, List<String> texts, List<String> colors) {

        if (texts == null || texts.size() < 1) {
            return;
        }
        if (colors == null || texts.size() > colors.size()) {
            return;
        }

        StringBuilder sb = new StringBuilder();

        ArrayList<ForegroundColorSpan> spans = new ArrayList<>();
        ArrayList<Integer> indexStarts = new ArrayList<>();
        ArrayList<Integer> indexEnds = new ArrayList<>();

        for (int i = 0; i < texts.size(); i++) {
            String str = texts.get(i);
            sb.append(texts.get(i));

            ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor(colors.get(i)));

            int indexStart = sb.length() - str.length();
            int indexEnd = indexStart + str.length();
            indexStarts.add(indexStart);
            indexEnds.add(indexEnd);

            spans.add(span);
        }

        SpannableStringBuilder ssb = new SpannableStringBuilder(sb.toString());

//        Spanned.SPAN_INCLUSIVE_EXCLUSIVE 从起始下标到终了下标，包括起始下标
//        Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到终了下标，同时包括起始下标和终了下标
//        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终了下标，但都不包括起始下标和终了下标
//        Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到终了下标，包括终了下标
        for (int i = 0; i < texts.size(); i++) {

            ssb.setSpan(spans.get(i), indexStarts.get(i), indexEnds.get(i), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        }
        textview.setText(ssb);
    }


}
